﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.Finance
{
    public partial class ProjectPayment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ProjectPayment));
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {              
                BindGv();
                BindDrop();
            }
        }

        private void BindGv()
        {
            gvList.DataSource = XIANGMU.GetNonReceive(hidProName.Value, hidConstructionUnit.Value, hidEvaluationUnit.Value, hidMoney.Value);
            gvList.DataBind();
        }

        private void BindDrop()
        {
            ddlEvaluationUnit.DataSource = EvaluationUnit.GetList();
            ddlEvaluationUnit.DataBind();
            ddlEvaluationUnit.Items.Insert(0, new ListItem("请选择", ""));
        }

        private void StatisticsMoney()
        {
            double SumMoney = 0;
            foreach (GridViewRow row in gvList.Rows)
            {
                if (((CheckBox)row.FindControl("CheckBox1")).Checked == true)
                {
                    SumMoney += double.Parse(row.Cells[9].Text);
                }
            }
            if (SumMoney > 0)
                Label1.Text = "应收总额" + SumMoney + "万元";
            else
                Label1.Text = string.Empty;
        }

        protected void gvList_PageIndexChanged(object sender, EventArgs e)
        {
            BindGv();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            hidConstructionUnit.Value = txtConstructionUnit.Text.Trim();
            if (ddlEvaluationUnit.SelectedIndex != 0)
                hidEvaluationUnit.Value = ddlEvaluationUnit.SelectedItem.Text;
            else
                hidEvaluationUnit.Value = string.Empty;
            hidMoney.Value = txtMoney.Text.Trim();
            hidProName.Value = txtProName.Text.Trim();
            BindGv();
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtConstructionUnit.Text = string.Empty;
            txtMoney.Text = string.Empty;
            txtProName.Text = string.Empty;
            ddlEvaluationUnit.SelectedIndex = 0;
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvList.Rows)
            {
                ((CheckBox)row.FindControl("CheckBox1")).Checked = true;
            }
            StatisticsMoney();
        }

        protected void btnUnselect_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvList.Rows)
            {
                if (((CheckBox)row.FindControl("CheckBox1")).Checked == false)
                {
                    ((CheckBox)row.FindControl("CheckBox1")).Checked = true;
                }
                else
                {
                    ((CheckBox)row.FindControl("CheckBox1")).Checked = false;
                }
            }
            StatisticsMoney();
        }

        protected void btnReciveMoney_Click(object sender, EventArgs e)
        {
            string ProjectID = string.Empty;
            foreach (GridViewRow row in gvList.Rows)
            {
                if (((CheckBox)row.FindControl("CheckBox1")).Checked == true)
                    ProjectID += gvList.DataKeys[row.RowIndex].Value + ",";
            }
            if (ProjectID == string.Empty)
            {
                Jscript.Alert("至少应选择一个项目！", this);
                return;
            }
            if (XIANGMU.ReceiveMoney(ProjectID.TrimEnd(',')))
            {
                BindGv();
                StatisticsMoney();
            }
            else
                Jscript.Alert("收款失败！", this);
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            StatisticsMoney();
        }

        protected void gvList_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(gvList);
        }
    }
}
